package com.link.base.base.duty.controller;

import com.link.base.enterprise.model.CoreEnterprise;
import com.link.base.user.model.CoreUser;
import com.link.base.base.user.model.User;
import com.link.base.base.auth.service.AuthclientService;
import com.link.base.base.common.model.InterObject;
import com.link.base.base.common.model.Menu;
import com.link.base.base.common.model.MenuTree;
import com.link.base.base.duty.model.Duty;
import com.link.base.base.duty.model.DutyDto;
import com.link.base.base.duty.model.DutyMenu;
import com.link.base.base.duty.service.DutyService;
import com.link.base.base.gateway.service.GatewayService;
import com.link.base.base.urlinfo.model.UrlInfo;
import com.link.base.base.urlinfo.service.UrlDutyLinkService;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.*;

/**
 * 职责(角色）模块
 *
 * @author unknwon
 * @version 1.0
 * @date: 2018/11/9 16:43
 */

@Controller
@RequestMapping("/link/duty")
public class DutyController extends BasicController<Duty> {


    @Resource
    private DutyService dutyService;

    @Resource
    private UrlDutyLinkService urlDutyLinkService;

    @Resource
    private AuthclientService authclientService;

    @Resource
    private GatewayService gatewayService;


    @Override
    public BasicService<Duty> getBasicService() throws Exception {
        return dutyService;
    }

    /**
     * 根据职责查询菜单列表，并将结果组织为属性结构返回
     *
     * @param record
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getMenuItemByDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMenuItemByDuty(@JsonParam Menu record, HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);

        try {
            record.setPageFlag(false);
            result = dutyService.menuTreeOfDuty(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 深度复制
     */
    @RequestMapping(value = "/deepUpsert")
    @ResponseBody
    public Map<String, Object> deepUpsert(@JsonParam QueryParams qps, @JsonParam Duty entity, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            entity.setRow_status("NEW");
            beforUpsert(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!getBasicService().isInsertFlag(entity) && !checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-002");
            }


            entity.setId(null);
            entity.setName(entity.getName() + "--copy");
            getBasicService().upsert(entity);

            Long newDutyId = entity.getId();

            Menu m = new Menu();
            Long dutyId = entity.getId();
            m.setDutyId(dutyId);
            m.setMenuGrade("1");
            List<Menu> menuList = dutyService.queryMenuByDutyId(m);
            for (Menu menu : menuList) {
                menu.setDutyId(newDutyId);
                menu.setRow_status("NEW");
                //修改深度复制bug --huanglj 2018年07月19日
                //修改深度复制bug
                menu.setId(Long.parseLong(menu.getAttr1()));
                dutyService.addMenuOfDutyInsert(menu);

                Menu subm = new Menu();
                subm.setDutyId(dutyId);
                subm.setMenuGrade("2");
                Long parentId = Long.parseLong(menu.getAttr1());
                subm.setParentMenuId(parentId);
                List<Menu> subMenuList = dutyService.queryMenuByDutyId(subm);
                for (Menu menu2 : subMenuList) {
                    menu2.setParentMenuId(menu.getId());
                    menu2.setDutyId(entity.getId());
                    menu2.setRow_status("NEW");
                    //修改深度复制bug --huanglj 2018年07月19日
                    menu2.setId(Long.parseLong(menu2.getAttr1()));
                    dutyService.addMenuOfDutyInsert(menu2);
                }


            }


            Duty newEntry = getBasicService().queryById(entity);
            result.put("newRow", newEntry);
            result.put("success", true);
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询职责列表，如果是000账套，则去除cropid限制
     */
    @RequestMapping(value = "/queryDutyListPage")
    @ResponseBody
    public Map<String, Object> queryDutyListPage(@JsonParam QueryParams qps, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            Duty t = (Duty) BasicModel.transformClass(Duty.class, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<Duty> list = dutyService.queryDutyListPage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Duty>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 导出职责列表
     */
    @RequestMapping({"/queryDutyListPage/exportData"})
    @ResponseBody
    public Map<String, Object> queryDutyListPageExportData(@JsonParam Duty entity, @JsonParam QueryParams qps,
                                                           HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessage("导出列表...");
        HashMap result = new HashMap(4);

        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            if (null != user) {
                user.setExportType(qps.getExportFileType());
            }

            ExportWriter exportWriter;
            if ("xlsx".equals(user.getExportType())) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }

            Duty temp = (Duty) BasicModel.transformClass(Duty.class, qps);
            while (!qps.getStopExport()) {

                List<Duty> list = dutyService.queryDutyListPage(temp);
                if (qps.getPage() == 1) {
                    qps.setTotal(temp.getTotal());
                }

                exportWriter.wirteData(list);
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }

                temp.setPage(qps.getPage());
                temp.setTotalFlag(qps.getTotalFlag());
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
            result.put("code", "200");
        } catch (BasicServiceException var11) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var11.getMessage());
            result.put("detailMessage", var11.getDetailMessage());
            result.put("code", var11.getCode());
        } catch (Exception var12) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var12.getMessage());
            result.put("code", "400");
        }

        return result;
    }

    /**
     * 查询职责列表，分页查询菜单关联的职责
     */
    @RequestMapping(value = "/queryDutyListOfMenu")
    @ResponseBody
    public Map<String, Object> queryDutyListOfMenu(@JsonParam QueryParams qps, HttpSession session,
                                                   HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            qps.invokePreFilterAndSorter();
            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);
            List<Duty> list = dutyService.queryDutyListOfMenu(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Duty>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 根据职责ID查询二级菜单,用于职责页面中的菜单列表显示
     */
    @RequestMapping(value = "/dutyOfMenuList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> dutyOfMenuList(@JsonParam QueryParams qps, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Menu> list = new ArrayList<Menu>();
        try {
            qps.preDealSecurity(request);
            qps.invokePreFilterAndSorter();
            List<Filter> filterList = qps.getFilterByProperty("dutyId");
            if (filterList == null || filterList.size() == 0) {
                qps.addFilter(new Filter("dutyId", "noMatchId"));
            }
            qps.addFilter(new Filter("menuGrade", "1"));
            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);
            list = dutyService.queryDutyOfMenuListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", list);
            result.put("total", qps.getTotal());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 导出菜单列表
     */
    @RequestMapping({"/dutyOfMenuList/exportData"})
    @ResponseBody
    public Map<String, Object> dutyOfMenuListExportData(@JsonParam Menu entity, @JsonParam QueryParams qps,
                                                        HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessage("导出列表...");
        HashMap result = new HashMap(4);

        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            if (null != user) {
                user.setExportType(qps.getExportFileType());
            }

            ExportWriter exportWriter;
            if ("xlsx".equals(user.getExportType())) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }

            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);

            while (!qps.getStopExport()) {
                List<Menu> list = dutyService.queryDutyOfMenuListPage(t);
                if (qps.getPage() == 1) {
                    qps.setTotal(t.getTotal());
                }

                exportWriter.wirteData(list);
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }

                t.setPage(qps.getPage());
                t.setTotalFlag(qps.getTotalFlag());
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
            result.put("code", "200");
        } catch (BasicServiceException var11) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var11.getMessage());
            result.put("detailMessage", var11.getDetailMessage());
            result.put("code", var11.getCode());
        } catch (Exception var12) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var12.getMessage());
            result.put("code", "400");
        }

        return result;
    }

    @RequestMapping(value = "/subMenuDutySelect", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> subMenuDutyPage(@JsonParam QueryParams qps, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Menu> list = new ArrayList<Menu>();
        try {
            qps.preDealSecurity(request);
            qps.invokePreFilterAndSorter();
            List<Filter> filterList = qps.getFilterByProperty("dutyId");
            if (filterList == null || filterList.size() == 0) {
                qps.addFilter(new Filter("dutyId", "noMatchId"));
            }
            List<Filter> parentMenuIdfilter = qps.getFilterByProperty("parentMenuId");
            if (parentMenuIdfilter == null || parentMenuIdfilter.size() == 0) {
                qps.addFilter(new Filter("parentMenuId", "noMatchId"));
            }
            qps.addFilter(new Filter("menuGrade", "2"));
            qps.setPageFlag(false);
            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);
            list = dutyService.queryDutyOfMenuListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", list);
            result.put("total", qps.getTotal());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/dutyOfUserList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> dutyOfUserList(@JsonParam User record, @JsonParam QueryParams qps, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<User> list = null;
        try {
            qps.invokePreFilterAndSorter();
            List<Filter> filterList = qps.getFilterByProperty("dutyId");
            if (filterList == null || filterList.size() == 0) {
                qps.addFilter(new Filter("dutyId", "noMatchId"));
            }
            User t = (User) BasicModel.transformClass(User.class, qps);
            list = dutyService.queryDutyOfUserListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 导出用户列表
     */
    @RequestMapping({"/dutyOfUserList/exportData"})
    @ResponseBody
    public Map<String, Object> dutyOfUserListExportData(@JsonParam User entity, @JsonParam QueryParams qps,
                                                        HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessage("导出列表...");
        HashMap result = new HashMap(4);

        try {
            qps.preDealSecurity(request);
            CoreUser user = UserUtil.getUser();
            if (null != user) {
                user.setExportType(qps.getExportFileType());
            }

            ExportWriter exportWriter;
            if ("xlsx".equals(user.getExportType())) {
                exportWriter = ExportFactory.newExcelInstance(qps);
            } else {
                exportWriter = ExportFactory.newInstance(qps);
            }

            User t = (User) BasicModel.transformClass(User.class, qps);

            while (!qps.getStopExport()) {
                List<User> list = dutyService.queryDutyOfUserListPage(t);
                if (qps.getPage() == 1) {
                    qps.setTotal(t.getTotal());
                }

                exportWriter.wirteData(list);
                if ("Y".equals(qps.getExportCurrentPage())) {
                    break;
                }

                t.setPage(qps.getPage());
                t.setTotalFlag(qps.getTotalFlag());
            }

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
            result.put("code", "200");
        } catch (BasicServiceException var11) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var11.getMessage());
            result.put("detailMessage", var11.getDetailMessage());
            result.put("code", var11.getCode());
        } catch (Exception var12) {
            LCLogger.info().withMessage("BasicController捕获异常: ");
            result.put("success", false);
            result.put("result", var12.getMessage());
            result.put("code", "400");
        }

        return result;
    }

    /**
     * 查询职责列表，如果是000账套，则去除cropid限制
     */
    @RequestMapping(value = "/usersDutyListPage")
    @ResponseBody
    public Map<String, Object> usersDutyListPage(@JsonParam DutyDto dto ,@JsonParam QueryParams qps, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            DutyDto t = BasicModel.transformClass(DutyDto.class, qps);
            t.setUsernames(dto.getUsernames());
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<DutyDto> list = dutyService.queryUsersDutyListPage(t);
            result.put("success", true);
            result.put("rows", list);
//            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Duty>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/unSelectMenuList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectMenuList(@JsonParam Duty record, @JsonParam QueryParams qps, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Menu> list = null;
        qps.setAttr1(String.valueOf(record.getId()));
        try {
            CoreEnterprise enterprise = SecurityInterceptor.tempEnterpirse.get();
            String industryVersion = enterprise.getIndustryVersion();
            if (StringUtils.isBlank(industryVersion)) {
                throw new ServiceException("DUTY-001");
            }
            /* 注意，industryVersion(企业表里的行业版本字段的值)与菜单表相应的行业字段必须一一对应，对应关系请
             * 对照Menu对象的相应字段和值列表中类型为INDUSTRY_VERSION的记录的独立源代码
             */

            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);
            Field f = t.getClass().getDeclaredField(industryVersion);
            f.setAccessible(true);
            f.set(t, "Y");
            list = dutyService.queryUnSelectMenuListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/unSelectSubMenuList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectSubMenuList(String dutyId, String menuItemId, @JsonParam QueryParams qps,
                                                   HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Menu> list = null;
        try {
            qps.setAttr1(dutyId);
            qps.addFilter(new Filter("parentMenuId", menuItemId));
            Menu t = (Menu) BasicModel.transformClass(Menu.class, qps);
            list = dutyService.queryUnSelectSubMenuListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * @param menuId       二级菜单Id
     * @param platformType 职责默认平台
     * @author zhangyuhao
     * @time 2019-2-18 11:10:58
     * @apiNote 查询未被 当前二级菜单关联的 职责列表
     */

    @RequestMapping(value = "/unSelectDutyOfMenu", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectDutyOfMenu(Long menuId, String platformType, @JsonParam QueryParams qps,
                                                  HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Duty> list = null;
        try {
            Duty t = (Duty) BasicModel.transformClass(Duty.class, qps);
            t.setMenuId(menuId);
            t.setPlatformType(platformType);
            list = dutyService.queryunSelectDutyOfMenuPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 查询未被当前url关联的职责列表
     *
     * @param record 职责
     * @author yaojiangyi
     * @date 2019/02/28 16:53
     */
    @RequestMapping(value = "/unSelectDutyOfUrl", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectDutyOfUrl(@JsonParam Duty record, @JsonParam QueryParams qps,
                                                 HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<Duty> list = null;
        try {
            Duty t = (Duty) BasicModel.transformClass(Duty.class, qps);
            t.setPlatformType(record.getPlatformType());
            list = dutyService.queryunSelectDutyOfUrlPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/addMenuOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addMenuOfDuty(@RequestBody List<InterObject> list, QueryParams qps,
                                             HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            for (int i = 0; list != null && i < list.size(); i++) {
                InterObject record = list.get(i);
                Menu menu = new Menu();
                menu.setDutyId(record.getPreObjId());
                menu.setId(record.getRearObjId());
                dutyService.addMenuOfDutyInsert(menu);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delMenuOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delMenuOfDuty(@JsonParam InterObject record, @JsonParam QueryParams qps, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            dutyService.delMenuOfDuty(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/unSelectUserList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelectUserList(@JsonParam User record, @JsonParam QueryParams qps, HttpSession session,
                                                HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<User> list = null;
        qps.setAttr1(String.valueOf(record.getId()));
        try {
            User t = (User) BasicModel.transformClass(User.class, qps);
            list = dutyService.queryUnSelectUserListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/addUserOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addUserOfDuty(@RequestBody List<InterObject> list, QueryParams qps,
                                             HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            for (int i = 0; list != null && i < list.size(); i++) {
                InterObject record = list.get(i);
                User user = new User();
                user.setDutyId(record.getPreObjId());
                user.setId(record.getRearObjId());
                dutyService.addUserOfDutyInsert(user);
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/delUserOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delUserOfDuty(@JsonParam InterObject record, @JsonParam QueryParams qps, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            dutyService.delUserOfDuty(record);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>更新职责菜单表</p>
     *
     * @param record
     * @param session
     * @author yrf
     */
    @RequestMapping(value = "/updateMenuOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateUserOfDuty(@JsonParam Menu record, HttpSession session) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            dutyService.menuOfDutyUpdate(record);
            record.setPageFlag(false);
            List<Menu> list = dutyService.queryDutyOfMenuListPage(record);
            if (list != null && list.size() == 1) {
                result.put("newRow", list.get(0));
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>批量更新职责下菜单信息</p>
     *
     * @param list
     * @param session
     * @author yrf
     */
    @RequestMapping(value = "/batchUpdateMenuOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateUserOfDuty(@RequestBody List<Menu> list, HttpSession session) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            if (list != null && list.size() > 0) {
                for (Menu record : list) {
                    dutyService.menuOfDutyUpdate(record);
                }
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * <p>查询未关联职责的url</p>
     *
     * @param record
     * @param qps
     * @param session
     * @param request
     * @param response
     * @author yrf
     */
    @RequestMapping(value = "/unSelectUrlList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> unSelecturlList(@JsonParam UrlInfo record, @JsonParam QueryParams qps, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        List<UrlInfo> list = null;
        try {
            UrlInfo t = (UrlInfo) BasicModel.transformClass(UrlInfo.class, qps);
            t.setType(record.getType());
            list = dutyService.queryUnSelectUrlListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * picklist查询000公司帐套信息
     *
     * @param qps
     * @param entity
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/queryStandardPage")
    @ResponseBody
    public Map<String, Object> queryStandardPage(@JsonParam QueryParams qps, @JsonParam Duty entity, HttpSession session,
                                                 HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            Duty t = (Duty) BasicModel.transformClass(entity.getClass(), qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            t.setLoginCorpId(0L);
            List<Duty> list = getBasicService().queryByExamplePage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<Duty>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 更新redis中职责与用户以及职责与URL的对应关系
     *
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/updatePermissions")
    @ResponseBody
    public Map<String, Object> updatePermissions(HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            dutyService.refreshPermissionsNew();
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 刷新单条职责的权限信息
     *
     * @author 黄龙建
     * @date 2020年07月07日
     */
    @RequestMapping(value = "/refreshPermByDutyId")
    @ResponseBody
    public Map<String, Object> refreshPermByDutyId(@JsonParam Duty duty, HttpSession session, HttpServletRequest request,
                                                 HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(4);
        try {
            if (duty.getId() == null) {
                throw new Exception("职责ID不能为空！");
            }
            dutyService.refreshPermByDutyId(duty);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 给当前职责添加菜单及安全性菜单
     * 先删除当前职责下的所有菜单及安全性菜单
     *
     * @param entity
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/addMenuAndSubMenuOfDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addMenuAndSubMenuOfDuty(@JsonParam DutyMenu entity, @JsonParam QueryParams qps,
                                                       HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<>(4);
        try {
            dutyService.addMenuAndSubMenuOfDuty(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 快速设置职责的菜单安全性
     *
     * @param entity 更新前数据和更新后数据
     * @author yangxiaolin
     * @date 2019/03/07 14:29
     */
    @RequestMapping(value = "/saveQuickSafetyMenuDuty", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveQuickSafetyMenuDuty(@RequestBody MenuTree entity, QueryParams qps,
                                                       HttpSession session, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> result = new HashMap<>(4);
        try {
            dutyService.saveQuickSafetyMenuDuty(entity);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
